﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Collections;
using Tomers.WPF.Localization;
using System.Globalization;
using Daphne.Forms;

namespace Daphne
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const string sThemePath = "themes";

        private static string _sLanguagePath = "AppData/Languages";


        public static string[] GetResourceNames()
        {
            var asm = Assembly.GetEntryAssembly();
            string resName = asm.GetName().Name + ".g.resources";
            using (var stream = asm.GetManifestResourceStream(resName))
            using (var reader = new System.Resources.ResourceReader(stream))
            {
                return reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray();
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;

            string[] resources = GetResourceNames();
            resources = resources.Where(r => r.Contains(sThemePath)).ToArray();           
            

            Resources.MergedDictionaries.Clear();
            foreach (string file in resources)
            {
                ResourceDictionary dictionary = LoadComponent(new Uri(string.Format("Daphne;component/{0}", file), UriKind.Relative))
                        as ResourceDictionary;

                if (dictionary != null)
                {
                    Resources.MergedDictionaries.Add(dictionary);
                }

            }

            // initialize global configuration
            string sAppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            String appData =
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            Config.InitConfig(appData);

            // init localization
            string sLangPath = Path.Combine(sAppPath, _sLanguagePath);

            // register dictionaries
            LanguageDictionary.RegisterDictionary(
                CultureInfo.GetCultureInfo("en-US"),
                new XmlLanguageDictionary(Path.Combine(sLangPath, "en-US.xml")));

            LanguageDictionary.RegisterDictionary(
                CultureInfo.GetCultureInfo("cs-CZ"),
                new XmlLanguageDictionary(Path.Combine(sLangPath, "cs-CZ.xml")));

            LanguageDictionary.RegisterDictionary(
                CultureInfo.GetCultureInfo("es-ES"),
                new XmlLanguageDictionary(Path.Combine(sLangPath, "es-ES.xml")));

            LanguageDictionary.RegisterDictionary(
                CultureInfo.GetCultureInfo("es-MX"),
                new XmlLanguageDictionary(Path.Combine(sLangPath, "es-LA.xml")));

            // set the current language
            LanguageContext.Instance.Culture = CultureInfo.GetCultureInfo("en-US");

            //Splash form = new Splash();
            //form.Show();
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            Forms.FatalErrorDialog dlg = new Forms.FatalErrorDialog();
            dlg.SetException(e.Exception);
            dlg.SetCulture(LanguageContext.Instance.Culture.Name);
            dlg.ShowDialog();
        }
    }
}
